package com.huika.dealontop.control.myinfo.activity;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RelativeLayout;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.google.gson.reflect.TypeToken;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.huika.dealontop.GlobalApp;
import com.huika.dealontop.R;
import com.huika.dealontop.config.Constants;
import com.huika.dealontop.config.UrlConstants;
import com.huika.dealontop.control.base.BaseAct;
import com.huika.dealontop.control.myinfo.adapter.MyEvaluateAdapter;
import com.huika.dealontop.control.timelimit.activity.MemberEvaluateActicvity;
import com.huika.dealontop.support.domain.MyEvaluateBean;
import com.huika.dealontop.support.http.FormResultRequest;
import com.huika.dealontop.support.http.JSONWrapAjaxParams;
import com.huika.dealontop.support.http.RequestResult;
import com.huika.dealontop.views.TitleBarHelper;

import java.util.ArrayList;

/**
 * Description:我的评价
 * Created by zhouweiyong on 2015/11/16.
 */
public class MyEvaluateActivity extends BaseAct implements PullToRefreshBase.OnRefreshListener2,AdapterView.OnItemClickListener{
    private PullToRefreshListView ptrLv;
    private MyEvaluateAdapter myEvaluateAdapter;

    @Override
    public void setRootView() {
        setContentView(R.layout.myevalute_act);
    }

    @Override
    protected void initWidget() {
        TitleBarHelper titleBarHelper = new TitleBarHelper(this, R.string.title_my_evaluate);
        titleBarHelper.setLeftMsg("");
        ptrLv = (PullToRefreshListView) findViewById(R.id.ptrlv_ma);
        myEvaluateAdapter = new MyEvaluateAdapter(this);
        ptrLv.setAdapter(myEvaluateAdapter);
        ptrLv.setMode(PullToRefreshBase.Mode.BOTH);
        ptrLv.setOnRefreshListener(this);
        ptrLv.setOnItemClickListener(this);
       // myEvaluateAdapter.setGroup(testData());
        startOverallLoading(ptrLv);
        handler.sendEmptyMessageDelayed(0, 1000);
    }


    private void net() {
        JSONWrapAjaxParams ajaxParams = new JSONWrapAjaxParams();
        ajaxParams.putCommonTypeParam("userId", GlobalApp.getInstance().getUserId());
        ajaxParams.putCommonTypeParam("pageNo", CURRENT_PAGE);
        ajaxParams.putCommonTypeParam("pageSize", PAGE_SIZE);
        FormResultRequest<ArrayList<MyEvaluateBean>> request = new FormResultRequest<ArrayList<MyEvaluateBean>>(UrlConstants.MEMBEREVALUATELIST, new Response.Listener<RequestResult<ArrayList<MyEvaluateBean>>>() {

            @Override
            public void onResponse(RequestResult<ArrayList<MyEvaluateBean>> arg0) {
                ptrLv.onRefreshComplete();
                if (isOvarallLoading){
                    stopOverallLoading();
                }
                if (arg0.flag==1&&arg0.getRs()!=null){
                    if (arg0.getRs().size()>0){
                        if (arg0.getRs().size()<PAGE_SIZE){
                           ptrLv.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
                        }else{
                            ptrLv.setMode(PullToRefreshBase.Mode.BOTH);
                        }
                        if (CURRENT_PAGE==1){
                            myEvaluateAdapter.setGroup(arg0.getRs());
                        }else{
                            myEvaluateAdapter.addItems(arg0.getRs());
                        }
                    }else {
                        showNoContentView(ptrLv);
                    }
                }
            }
        },this, new TypeToken<RequestResult<ArrayList<MyEvaluateBean>>>() { }.getType());
        request.setRequestParams(ajaxParams);
        executeRequest(request);
    }



    @Override
    public void onErrorResponse(VolleyError error) {
        super.onErrorResponse(error);

    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            net();
        }
    };

    private ArrayList<MyEvaluateBean> testData() {
        ArrayList<MyEvaluateBean> list = new ArrayList<MyEvaluateBean>();
        for (int i = 0; i < 10; i++) {
            MyEvaluateBean myEvaluateBean = new MyEvaluateBean();
            myEvaluateBean.evaluateScore = "3.0";
            myEvaluateBean.evaluateTime = "1447666270";
            myEvaluateBean.evaluateContent = "中新网11月16日电 北京时间今天凌晨，奥运会拳击冠军邹市明在微博上晒出了他和妻子冉莹颖以及武打演员吴京的合照。照片中三人一袭黑衣，邹市明夫妇被吴京揽在怀中，在街头摆出炫酷的造型。邹市明在微博中说道，“来美国也逃不掉，近两个小时车程终于聚上，我们好友跟踪系统不是盖的！祝康复顺利！ @冉莹颖 @吳京";
            if (i % 2 == 1) {
                myEvaluateBean.merchantReply = "网友看后留言到，“你两要是打一架谁厉害”、“求偶遇！洛杉矶粉丝在这里！";
            }
            list.add(myEvaluateBean);
        }
        return list;
    }

    private void showNoContentView(ViewGroup viewGroup) {
        RelativeLayout noContentView = (RelativeLayout) LayoutInflater.from(this).inflate(R.layout.myevalute_no_content, null);
        Button btn_evaluate_mnc = (Button) noContentView.findViewById(R.id.btn_evaluate_mnc);
        btn_evaluate_mnc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MyEvaluateActivity.this, MyOrderListActivity.class);
                intent.putExtra(MyOrderListActivity.CONTENTKEY,3);
                showActivity(MyEvaluateActivity.this, intent);
                MyEvaluateActivity.this.finish();
            }
        });
        ViewGroup parentView = (ViewGroup) viewGroup.getParent();
        int index = parentView.indexOfChild(viewGroup);
        ViewGroup.LayoutParams params = viewGroup.getLayoutParams();
        noContentView.setLayoutParams(params);
        parentView.removeViewAt(index);
        parentView.addView(noContentView, index);
    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase refreshView) {
        CURRENT_PAGE = 1;
        net();
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase refreshView) {
        CURRENT_PAGE++;
        net();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        int realPos = position - ((ListView) parent).getHeaderViewsCount();
        if (realPos >= 0 && realPos < myEvaluateAdapter.getCount()) {
            MyEvaluateBean myEvaluateBean = myEvaluateAdapter.getItem(realPos);
            Intent intent = new Intent(this, MemberEvaluateActicvity.class);
            intent.putExtra(Constants.EVALUATE_ID,myEvaluateBean.evaluateID);
            showActivity(this,intent);
        }

    }
}
